package fragments;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import mindshare.JSONParser;
import mindshare.LoadContent;
import mindshare.MainActivity;
import mindshare.SessionManager;
import info.androidhive.slidingmenu.Notificador;
import info.androidhive.slidingmenu.R;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;

public class Question extends Activity {
	public static final String KEY_NAME = "name";
	private static final String QUESTION = "q";
	List<NameValuePair> param = new ArrayList<NameValuePair>();
	private ImageView sendQuestion;
	private ImageView ivCategory;
	private EditText question;
	private Spinner spCategory;
	JSONParser jParser = new JSONParser();
	ArrayAdapter<String> adpCategory;
	public List<String> categoryList = new ArrayList<String>();
	private int lastPosition = 0;
	List<NameValuePair> params = new ArrayList<NameValuePair>();
	View rootview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_question);
		question = (EditText) findViewById(R.id.question);
		spCategory = (Spinner) findViewById(R.id.spCategory);
		spCategory.setOnItemSelectedListener(new onItemSelect());
		params.clear();
		GetCategories loadCategories = new GetCategories(this, params,
				"category");
		loadCategories.execute();
		sendQuestion = (ImageView) findViewById(R.id.imageSend);
		sendQuestion.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sendQuestion.setAlpha(50);
				// syncQuestion();
				registerQuestion(v);
				Intent intent = new Intent(getApplicationContext(),MainActivity.class);
				startActivity(intent);
			}
		});
		ivCategory = (ImageView) findViewById(R.id.category);
		ivCategory.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				System.out
						.println("---------------------------CLICK---------------------------");
				spCategory.performClick();
			}
		});
	}

	class onItemSelect implements OnItemSelectedListener {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			if (lastPosition != position) {
				String categoryParam = parent.getItemAtPosition(position)
						.toString();
				params.clear();
				params.add(new BasicNameValuePair("category", categoryParam));
			}
			lastPosition = position;
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
		}
	}

	public String getQuestion() {
		String q = null;
		q = question.getText().toString(); // PREGUNTA
		// System.out.println(etQuestion);
		return q;
	}

	public String getCategory() {
		String userCategory = spCategory.getSelectedItem().toString();
		// System.out.println(userCategory);
		return userCategory;
	}

	public String getNickname() {
		String nickname = null;
		SessionManager session = new SessionManager(getApplicationContext());
		nickname = session.getUserDetails().get(KEY_NAME);
		return nickname;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.question, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

	}

	class GetCategories extends LoadContent {

		public GetCategories(Context context, List<NameValuePair> param,
				String data) {
			super(context, param, data);
		}

		@Override
		protected String doInBackground(String... args) {
			JSONObject json = jParser.makeHttpRequest(
					"http://mindshare.comli.com/obtainCategories.php", params);
			JSONArray categories;
			contentList.clear();
			try {
				categories = json.getJSONArray(data);
				for (int i = 0; i < categories.length(); i++) {
					String c = categories.getString(i);
					contentList.add(c);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String file_url) {
			adpContent = new ArrayAdapter<String>((Activity) c,
					android.R.layout.simple_list_item_1, android.R.id.text1,
					contentList);
			adpContent
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			Spinner spCategory = (Spinner) findViewById(R.id.spCategory);
			spCategory.setAdapter(adpContent);
			List<NameValuePair> category = new ArrayList<NameValuePair>();
			category.add(new BasicNameValuePair("category", "Science"));
			pDialog.dismiss();
		}

	}

	// REGISTER QUESTION ON DATABASE

	class RegistQuestion extends LoadContent {

		public RegistQuestion(Context context, List<NameValuePair> param,
				String data) {
			super(context, param, data);
			// TODO Auto-generated constructor stub
		}

		@Override
		protected String doInBackground(String... args) {
			JSONObject json = jParser.makeHttpRequest(
					"http://mindshare.comli.com/post.php", param);
			return null;
		}

	}

	public void registerQuestion(View v) {
		// EditText etNickname = (EditText)findViewById(R.id.name);
		Notificador p = new Notificador(this);
		
		String nickname = getNickname();
		String category = getCategory();
		String postType = QUESTION;
		String description = getQuestion();
		p.notificar(getNickname(), category, category + ": " + description);
		// param1.add(new BasicNameValuePair("nickname",
		// etNickname.getText().toString()));
		param.clear();
		param.add(new BasicNameValuePair("nickname", nickname));
		param.add(new BasicNameValuePair("post_type", postType));
		param.add(new BasicNameValuePair("category", category));
		param.add(new BasicNameValuePair("description", description));
		// param.add(new BasicNameValuePair("picture",));
		// Log.d("Param", param.toString());
		RegistQuestion q = new RegistQuestion(this, param, "");
		q.execute();
		// param.clear();
	}
}
